package com.example.clxy.controller;

import com.example.clxy.entity.Semester;
import com.example.clxy.interceptor.AppToken;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.SemesterPageResult;
import com.example.clxy.result.business.SemesterResult;
import com.example.clxy.result.business.SemesterResult2;
import com.example.clxy.service.SemesterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author liuhaoqi
 * @Date 2020/8/3 21:45
 * @Version 1.0
 */
@AppToken
@RestController
@RequestMapping("SemesterController")
public class SemesterController {
    @Autowired
    SemesterService semesterService;

    /**
     * 获取所有学期信息
     *
     * @param semestername
     * @param page
     * @param pagesize
     * @return
     */
    @CrossOrigin
    @RequestMapping("getSemesterList")
    public PageResult<SemesterPageResult> getSemesterList(String semestername, int page, int pagesize) {
        PageResult<SemesterPageResult> result = semesterService.getSemesterList(semestername, page, pagesize);
        return result;
    }

    /**
     * 新增学期
     *
     * @param semester
     * @return
     */
    @CrossOrigin
    @PostMapping("addSemester")
    public WebRestResult addSemester(@RequestBody Semester semester) {
        WebRestResult result = semesterService.addSemester(semester);
        return result;
    }

    /**
     * 修改学期
     *
     * @param semester
     * @return
     */
    @CrossOrigin
    @PostMapping("updateSemester")
    public WebRestResult updateSemester(@RequestBody Semester semester) {
        WebRestResult result = semesterService.updateSemester(semester);
        return result;
    }

    /**
     * 删除学期
     *
     * @param semesterid
     * @return
     */
    @CrossOrigin
    @PostMapping("deleteSemester")
    public WebRestResult deleteSemester(String semesterid) {
        WebRestResult result = semesterService.deleteSemester(semesterid);
        return result;
    }

    @CrossOrigin
    @GetMapping("getSemesterById")
    public SemesterResult getSemesterById(String semesterid) {
        SemesterResult result = semesterService.getSemesterById(semesterid);
        return result;
    }

    @CrossOrigin
    @GetMapping("getSemesterById2")
    public SemesterResult2 getSemesterById2(String semesterid) {
        SemesterResult2 result = semesterService.getSemesterById2(semesterid);
        return result;
    }

    /**
     * 根据日期获取当前最近的一个学期
     * @param date
     * @return
     */
    @CrossOrigin
    @GetMapping("getSemesterByDate")
    public SemesterResult getSemesterByDate(@DateTimeFormat(pattern = "yyyy-MM-dd") String date) {
        SemesterResult result = semesterService.getSemesterByDate(date);
        return result;
    }
}
